<?php
/**
 * Created by PhpStorm OF Administrator.
 * User: YUYI
 * Date: 2018/7/20 0020
 * Time: 上午 10:03
 * Administrator
 */

namespace app\common\util;


trait HuabanPic
{
    /**
     * 获取花瓣图片路径
     * @param $query string 查询关键词
     * @param int $page 第几页
     * @param int $size 每页图片张数
     */
    public function getPics($query,$page=1,$size=10)
    {
        header("Content-Type:text/html;charset=UTF-8");
        $showapi_appid = '70490';  //替换此值,在官网的"我的应用"中找到相关值
        $showapi_secret = 'ac91bad9310140109b44a31a08861b06';  //替换此值,在官网的"我的应用"中找到相关值
        $paramArr = array(
            'num' => $size
            ,'apiCode_pointCode' => '819-1'
            ,'showapi_userId' => '5b5524340cf26762812bc10f'
            ,'page' => $page
            ,'showapi_appid' => $showapi_appid
            ,'showapi_userName' => 'qq393855529'
            ,'showapi_userIp' =>'118.24.138.30'
            ,'type'=>$query
        );
        $param = $this->createParam($paramArr,$showapi_secret);
        $url = 'http://route.showapi.com/819-1?'.$param;
        $result = file_get_contents($url);
        trace($result,'post.result');
        $result = json_decode($result,1);
        if ($result['showapi_res_code'] == 0){
            return $result['showapi_res_body'];
        }
    }

    /**
     * 获取花瓣网图片
     * @param  string  $mixed         查询关键字或直接一个画板id
     * @param  int        $page_limit     要查询几页的，默认只查一页
     * @return array|null
     */
    function getHuabanImgs($query,$page=1,$size=10){
        header("Content-Type:text/html;charset=UTF-8");
        $showapi_appid = '70490';  //替换此值,在官网的"我的应用"中找到相关值
        $showapi_secret = 'ac91bad9310140109b44a31a08861b06';  //替换此值,在官网的"我的应用"中找到相关值
        $arr = [
            "欧美", "韩版", "日系", "英伦", "OL风", "学院", "淑女", "性感", "复古", "街头", "休闲", "民族", "甜美", "运动", "可爱", "大码", "中老年", "其他"
        ];
        $paramArr = array(
            'order'=> "1"
            ,'apiCode_pointCode' => '819-1'
            ,'showapi_userId' => '5b5524340cf26762812bc10f'
            ,'page' => $page
            ,'showapi_appid' => $showapi_appid
            ,'showapi_userName' => 'qq393855529'
            ,'showapi_userIp' =>'118.24.138.30'
            ,'type'=>$arr[$query]
        );
        $param = $this->createParam($paramArr,$showapi_secret);
        $url = 'http://route.showapi.com/126-2?'.$param;
        $result = file_get_contents($url);
        trace($result,'post.result');
        $result = json_decode($result,1);
        if ($result['showapi_res_code'] == 0){
            return $result['showapi_res_body'];
        }
    }

    protected function createParam($paramArr,$showapi_secret) {
        $paraStr = "";
        $signStr = "";
        ksort($paramArr);
        foreach ($paramArr as $key => $val) {
            if ($key != '' && $val != '') {
                $signStr .= $key.$val;
                $paraStr .= $key.'='.urlencode($val).'&';
            }
        }
        $signStr .= $showapi_secret;//排好序的参数加上secret,进行md5
        $sign = strtolower(md5($signStr));
        $paraStr .= 'showapi_sign='.$sign;//将md5后的值作为参数,便于服务器的效验
        return $paraStr;
    }
}